package com.qifenqian.turbot.module.adapter.marketing

import android.os.Build
import android.support.annotation.RequiresApi
import android.text.Editable
import android.text.TextWatcher
import android.view.Gravity
import android.view.View
import android.widget.PopupMenu
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.qifenqian.turbot.R
import com.qifenqian.turbot.module.entity.response.marketing.SpecialDishEntity
import com.qifenqian.turbot.util.Lcat
import kotlinx.android.synthetic.main.item_special_dish.view.*

/**
 * @author H
 * @createTime 2019/1/3 13:56
 * @description
 * @see
 * @since
 */
class SpecialDishRvAdapter(data: List<SpecialDishEntity.SpecialDish>) :
        BaseQuickAdapter<SpecialDishEntity.SpecialDish, BaseViewHolder>(R.layout.item_special_dish, data) {

    @RequiresApi(Build.VERSION_CODES.KITKAT)
    override fun convert(helper: BaseViewHolder, item: SpecialDishEntity.SpecialDish) {
        Lcat.a("SpecialDishRvAdapter", item)
        val view = helper.itemView
        val week: String = when (item.weekDay) {
            1    -> "星期一"
            2    -> "星期二"
            3    -> "星期三"
            4    -> "星期四"
            5    -> "星期五"
            6    -> "星期六"
            7    -> "星期日"
            else -> ""
        }
        view.tv_item_special_dish_week.text = week
        view.tv_item_special_dish_name.text = item.dishName
        view.et_item_special_dish_new_price_small.isEnabled = false
        view.et_item_special_dish_new_price_middle.isEnabled = false
        view.et_item_special_dish_new_price_big.isEnabled = false
        if (item.isSetSize == 0) {
            view.tv_item_special_dish_old_price_small.text = "${(item.price / 100)}元"
            view.et_item_special_dish_new_price_small.setText("${item.specialPrice / 100}")
            view.tv_item_special_dish_old_price_middle.visibility = View.GONE
            view.tv_item_special_dish_old_price_big.visibility = View.GONE
            view.et_item_special_dish_new_price_middle.visibility = View.GONE
            view.et_item_special_dish_new_price_big.visibility = View.GONE
        } else {
            view.tv_item_special_dish_old_price_middle.visibility = View.VISIBLE
            view.tv_item_special_dish_old_price_big.visibility = View.VISIBLE
            view.et_item_special_dish_new_price_middle.visibility = View.VISIBLE
            view.et_item_special_dish_new_price_big.visibility = View.VISIBLE
            view.tv_item_special_dish_old_price_small.text = "${(item.sizeList[0].price / 100)}元(小)"
            view.tv_item_special_dish_old_price_middle.text = "${(item.sizeList[1].price / 100)}元(中)"
            view.tv_item_special_dish_old_price_big.text = "${(item.sizeList[2].price / 100)}元(大)"
            view.et_item_special_dish_new_price_small.setText("${item.sizeList[0].specialPrice / 100}")
            view.et_item_special_dish_new_price_middle.setText("${item.sizeList[1].specialPrice / 100}")
            view.et_item_special_dish_new_price_big.setText("${item.sizeList[2].specialPrice / 100}")
        }
        view.tv_item_special_dish_name.isEnabled = view.tv_item_special_dish_name.text.toString() == "选择菜品"
        view.et_item_special_dish_new_price_small.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {
                if (s.toString().trim().isNotBlank()) {
                    if (item.isSetSize == 0) {
                        item.specialPrice = s!!.toString().toDouble() * 100
                    } else {
                        item.sizeList[0].specialPrice = s!!.toString().toDouble() * 100
                    }
                }
            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            }
        })
        view.et_item_special_dish_new_price_middle.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {
                if (s.toString().trim().isNotBlank()) {
                    if (!view.et_item_special_dish_new_price_small.text.toString().isBlank()) {
                        if (item.isSetSize == 1) {
                            item.sizeList[1].specialPrice = s!!.toString().toDouble() * 100
                        }
                    }
                }
            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            }
        })
        view.et_item_special_dish_new_price_big.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {
                if (s.toString().trim().isNotBlank()) {
                    if (!view.et_item_special_dish_new_price_small.text.toString().isBlank()) {
                        if (item.isSetSize == 1) {
                            item.sizeList[2].specialPrice = s!!.toString().toDouble() * 100
                        }
                    }
                }
            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            }
        })
        view.setOnLongClickListener {
            val popupMenu = PopupMenu(mContext, view, Gravity.END)
            popupMenu.menuInflater.inflate(R.menu.menu_edit_delete, popupMenu.menu)
            popupMenu.show()
            popupMenu.setOnMenuItemClickListener { item ->
                when (item.itemId) {
                    R.id.menu_edit   -> {
//                        view.tv_item_special_dish_name.isEnabled = true
//                        view.et_item_special_dish_new_price_small.isEnabled = true
//                        view.et_item_special_dish_new_price_middle.isEnabled = true
//                        view.et_item_special_dish_new_price_big.isEnabled = true
//                        view.tv_item_special_dish_name.setTextColor(mContext.resources.getColor(R.color.blue_e60080ff))
//                        view.et_item_special_dish_new_price_small.setTextColor(mContext.resources.getColor(R.color.gray_666666))
//                        view.et_item_special_dish_new_price_middle.setTextColor(mContext.resources.getColor(R.color.gray_666666))
//                        view.et_item_special_dish_new_price_big.setTextColor(mContext.resources.getColor(R.color.gray_666666))
                        mOnDishNameClickListener?.onEdit(helper.layoutPosition)
                        return@setOnMenuItemClickListener true
                    }
                    R.id.menu_delete -> {
                        mOnDishNameClickListener?.onDelete(helper.layoutPosition, view)
                        return@setOnMenuItemClickListener true
                    }
                    else             -> return@setOnMenuItemClickListener false
                }
            }
//            popupMenu.setOnDismissListener {
//                view.tv_item_special_dish_name.setTextColor(mContext.resources.getColor(R.color.gray_666666))
//                view.et_item_special_dish_new_price_small.isEnabled = false
//                view.et_item_special_dish_new_price_middle.isEnabled = false
//                view.et_item_special_dish_new_price_big.isEnabled = false
//            }
            return@setOnLongClickListener true
        }
    }

    fun setOnDishNameClickListener(onDishNameClickListener: OnDishNameClickListener) {
        mOnDishNameClickListener = onDishNameClickListener
    }

    private var mOnDishNameClickListener: OnDishNameClickListener? = null

    interface OnDishNameClickListener {
        /**
         * 编辑
         */
        fun onEdit(position: Int)

        /**
         * 删除
         */
        fun onDelete(position: Int, view: View)
    }
}

